using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_explore : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	private Tools t = new Tools();
	private int page;
	private int currentpage;
	private long amount;

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		int line = 0;
		//bool isLoggedIn = false;
		string imgtitle = "";

		Account tmp = new Account();

		List<ImageItem> items = new List<ImageItem>();

		HtmlTableRow row = null;
		HtmlTableRow row2 = null;
		HtmlTableRow row3 = null;

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			tmp = ap.GetAccountBySession(Session["Zappr"].ToString());
			//isLoggedIn = true;
		}

		// pages
		if(Request.QueryString.Get("page") == null) {
			page = 0;
			currentpage = 1;
		}
		else {
			page = Convert.ToInt32(Request.QueryString.Get("page").ToString());
			currentpage = page;
			page = t.ConvertPageToStartValue(page, 15);
		}

		// get images
		amount = ip.GetImageAmount();
		items.Clear();

		if(page == 0) {
			items = ip.GetImageList(page, 15, 15, true);
		}
		else {
			items = ip.GetImageList(page, page + 15, 15, true);
		}

		// list images
		foreach(ImageItem item in items) {
			if(line == 0) {
				row = new HtmlTableRow();
				row2 = new HtmlTableRow();
				row3 = new HtmlTableRow();
			}

			//
			// image
			//

			HtmlTableCell cell = new HtmlTableCell();
			cell.VAlign = "top";
			cell.Align = "center";
			cell.Width = "300";
			cell.Style.Add("width", "300px");

			// image
			HyperLink link = new HyperLink();
			link.ImageUrl = "../../Images/" + item.Image240;
			link.CssClass = "listTitle";
			//link.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
			link.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";
			link.Text = item.Name;

			cell.Controls.Add(link);

			// newline
			cell.Controls.Add(new LiteralControl("<br />"));

			// text
			/*Label text = new Label();
			text.CssClass = "listText";
			text.Text = item.Description;

			cell.Controls.Add(text);*/

			row.Cells.Add(cell);
			imageTable.Rows.Add(row);

			//
			// title
			//
			imgtitle = item.Name;

			if(imgtitle.Length > 26) {
				if(imgtitle.IndexOf(' ') == -1 || imgtitle.IndexOf(' ') > 25) {
					imgtitle = item.Name.Substring(0, 24) + "...";
				}
			}

			HtmlTableCell celltitle = new HtmlTableCell();
			celltitle.VAlign = "top";
			celltitle.Align = "center";
			celltitle.Width = "300";
			celltitle.Style.Add("width", "300px");

			// text
			HyperLink title = new HyperLink();
			title.CssClass = "normal";
			//title.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
			title.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";
			title.Text = imgtitle;

			celltitle.Controls.Add(title);

			row2.Cells.Add(celltitle);
			imageTable.Rows.Add(row2);

			//
			// footer
			//
			HtmlTableCell cellfooter = new HtmlTableCell();
			cellfooter.VAlign = "top";
			cellfooter.Align = "center";
			cellfooter.Width = "300";
			cellfooter.Style.Add("width", "300px");

			// text
			Label footer10 = new Label();
			footer10.CssClass = "listFooter";
			footer10.Text += GetLocalResourceObject("UploadedOn").ToString() + " ";
			footer10.Text += item.UploadDate.ToShortDateString();
			footer10.Text += " ";
			footer10.Text += GetLocalResourceObject("by").ToString() + " ";

			cellfooter.Controls.Add(footer10);

			// text
			HyperLink footer15 = new HyperLink();
			footer15.CssClass = "listFooter";
			//footer15.NavigateUrl = "~/default.aspx?id=images&user=" + item.User.Username;
			footer15.NavigateUrl = "~/images/" + item.User.Username + "/default.aspx";
			footer15.Text += item.User.Username;

			cellfooter.Controls.Add(footer15);

			// newline
			cellfooter.Controls.Add(new LiteralControl("<div style=\"height: 25px; display: block;\"></div>"));

			row3.Cells.Add(cellfooter);
			imageTable.Rows.Add(row3);

			//
			// compute
			//
			line++;

			if(line == 3) {
				row = null;
				row2 = null;
				row3 = null;
				
				line = 0;
			}
		}

		if(amount > 15 || page > 0) {
			this.DisplayPageList(Convert.ToInt32(amount));
		}
	}

	/// <summary>
	/// Display the pages on the footer
	/// </summary>
	/// <param name="amount"></param>
	private void DisplayPageList(int amount) {
		double tmp = System.Math.Round((Convert.ToDouble(amount ) / 15), 0, MidpointRounding.AwayFromZero);
		long pages = Convert.ToInt64(tmp) + 1;
		int counter = 0;

		// pages
		HyperLink link = null;

		if(pages > 1) {
			plcFooter.Controls.Add(new LiteralControl("<br />"));

			if(currentpage != 1) {
				link = null;
				link = new HyperLink();
				link.CssClass = "pageStep";
				link.NavigateUrl = "~/default.aspx?id=explore";
				link.NavigateUrl += "&page=" + (currentpage - 1).ToString();
				link.Text = GetLocalResourceObject("Previous").ToString();

				plcFooter.Controls.Add(link);
			}

			for(int i = 1; i <= pages; i++) {
				link = new HyperLink();

				if(i == currentpage) {
					link.CssClass = "pageCurrentStep";
				}
				else {
					link.CssClass = "pageStep";
				}

				link.NavigateUrl = "~/default.aspx?id=explore";
				link.NavigateUrl += "&page=" + i.ToString();
				link.Text = i.ToString();

				// aktiver link ist "page"

				plcFooter.Controls.Add(link);

				plcFooter.Controls.Add(new LiteralControl("&nbsp;"));

				link = null;
				counter++;
			}

			if(currentpage < counter) {
				link = null;
				link = new HyperLink();
				link.CssClass = "pageStep";
				link.NavigateUrl = "~/default.aspx?id=explore";
				link.NavigateUrl += "&page=" + (currentpage + 1).ToString();
				link.Text = GetLocalResourceObject("Next").ToString();

				plcFooter.Controls.Add(link);
			}
		}
	}
}
